<% layout('/layouts/default.html', {title: '客户结算单查询', libs: ['validate','dataGrid']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text('客户结算单详情')}
			</div>
			<div class="box-tools pull-right">
				<% if(hasPermi('settlement:customersettlement:customerSettlement:contact')){ %>
					<button type="button" class="btn btn-sm btn-primary" id="btnS" onclick="supContract()">${text('供需合同')}</button>
				<% } %>
				<a href="${ctx}/settlement/customerCarContract/createFromSettle?customerSettlementId=${customerSettlement.customerSettlementId}" id="btnSh"
				   class="btn btn-primary addTabPage" title="${text('供需合同实际提交')}" style="display:none;"></a>
				<% if(hasPermi('settlement:customersettlement:customerSettlement:complete')){ %>
					<button type="button" class="btn btn-sm btn-primary" id="btnF" onclick="calFinish()">${text('结算完结')}</button>&nbsp;
				<% } %>
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${customerSettlement}" action="${ctx}/settlement/customersettlement/customerSettlement/save" method="post" class="form-horizontal">
		<div class="box-body">
			<div class="form-unit">${text('基本信息')}</div>
			<#form:hidden path="customerSettlementId"/>
			<div class="row">
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('编号')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="customerSettlementCode" maxlength="100" class="form-control" readonly="true"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('客户')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:select path="customerId" items="${customerList}" itemLabel="customerName"
							itemValue="customerId"  class="form-control required" disabled="true"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required ">*</span> ${text('结算日期')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="date" readonly="true" maxlength="20" class="form-control laydate required"
							dataFormat="date" data-type="date" data-format="yyyy-MM-dd" disabled="true"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('开票客户')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:select path="invoiceCustomerId" items="${customerList}" itemLabel="customerName"
							itemValue="customerId"  class="form-control required" disabled="true"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('供应链服务协议')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="supplyChainServiceAgreementId" maxlength="64" class="form-control" readonly="true"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('服务订单')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="serviceOrderId" maxlength="64" class="form-control" readonly="true"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('付款方式')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:select path="paymentMethod" dictType="payment_method" blankOption="true" class="form-control" disabled="true"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('信用证')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="letterCreditCode" maxlength="100" class="form-control" readonly="true"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('是否结算完结')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:radio path="finishFlag" dictType="sys_yes_no" class="form-control" disabled="true"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('客户预收款余额')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="customerPrepaymentBalance" class="form-control number" readonly="true"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required hide">*</span> ${text('订单预收款余额')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="orderPrepaymentBalance" class="form-control number" readonly="true"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xs-12">
					<div class="form-group">
						<label class="control-label col-sm-2" title="">
							<span class="required hide">*</span> ${text('备注信息')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-10">
							<#form:textarea path="remarks" rows="4" maxlength="500" class="form-control" readonly="true"/>
						</div>
					</div>
				</div>
			</div>
			<h4 class="form-unit">${text('合计信息')}</h4>
			<div class="ml10 mr10">
				<table id="customerSettlementDetailDataGrid"></table>
			</div>
			<h4 class="form-unit">${text('预收款信息')}</h4>
			<div class="ml10 mr10">
				<table id="customerSettlementPrepaymentDataGrid"></table>
			</div>
			<h4 class="form-unit">${text('收款明细信息')}</h4>
			<div class="ml10 mr10">
				<table id="customerSettlementPaymentDataGrid"></table>
			</div>
			<h4 class="form-unit">${text('系统信息')}</h4>
			<div class="col-xs-6">
				<div class="form-group">
					<label class="control-label col-sm-4" title="">
						<span class="required hide">*</span> ${text('创建者')}：<i class="fa icon-question hide"></i></label>
					<div class="col-sm-8">
						<#form:input path="createBy" maxlength="64" class="form-control" disabled="true"/>
					</div>
				</div>
			</div>
			<div class="col-xs-6">
				<div class="form-group">
					<label class="control-label col-sm-4" title="">
						<span class="required hide">*</span> ${text('创建时间')}：<i class="fa icon-question hide"></i></label>
					<div class="col-sm-8">
						<#form:input path="createDate" readonly="true" maxlength="20" class="form-control" disabled="true"
						dataFormat="date" data-type="date" data-format="yyyy-MM-dd"/>
					</div>
				</div>
			</div>
			<div class="col-xs-6">
				<div class="form-group">
					<label class="control-label col-sm-4" title="">
						<span class="required hide">*</span> ${text('更新者')}：<i class="fa icon-question hide"></i></label>
					<div class="col-sm-8">
						<#form:input path="updateBy" maxlength="64" class="form-control" disabled="true"/>
					</div>
				</div>
			</div>
			<div class="col-xs-6">
				<div class="form-group">
					<label class="control-label col-sm-4" title="">
						<span class="required hide">*</span> ${text('更新时间')}：<i class="fa icon-question hide"></i></label>
					<div class="col-sm-8">
						<#form:input path="updateDate" readonly="true" maxlength="20" class="form-control" disabled="true"
						dataFormat="date" data-type="date" data-format="yyyy-MM-dd"/>
					</div>
				</div>
			</div>
		</div>
		<div class="box-footer">
			<div class="row">
				<div class="col-sm-offset-2 col-sm-10">
					<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
				</div>
			</div>
		</div>
	</#form:form>
</div>
</div>
<% } %>
<script>
//初始化客户结算单付款DataGrid对象
$("#customerSettlementPaymentDataGrid").dataGrid({

	data: ${toJson(customerSettlement.customerSettlementPaymentList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度

	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'customerSettlementPaymentId', editable:true, hidden:true},
		{header:'${text("车辆")}', name:'carId', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'64', 'class':'form-control'}},
		{header:'${text("车架号")}', name:'frameNumber', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'100', 'class':'form-control'}},
		{header:'${text("收款单号")}', name:'paymentCode', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'100', 'class':'form-control'}},
		{header:'${text("费用项目")}', name:'expenseItem', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'2', 'class':'form-control'}},
		{header:'${text("实收金额")}', name:'actualAmount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number'}},
		{header:'${text("是否是核销预收款")}', name:'ifFlag', width:100,
			editable:true, edittype:'select', editoptions:{'class':'form-control',
				items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], ${@DictUtils.getDictListJson('sys_yes_no')}),
				itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
				}
			}
		},
		{header:'${text("客户结算单ID")}', name:'customerSettlementId.customerSettlementId', editable:true, hidden:true}
	],

	showFooter: true,

	// 编辑表格参数
	editGrid: false,				// 是否是编辑表格
	editGridInitRowNum: 1,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#customerSettlementPaymentDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {customerSettlementPaymentId: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据

	// 编辑表格的提交数据参数
	editGridInputFormListName: 'customerSettlementPaymentList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,customerSettlementPaymentId,carId,frameNumber,paymentCode,expenseItem,actualAmount,ifFlag,customerSettlementId.customerSettlementId,', // 提交数据列表的属性字段

	// 加载成功后执行事件
	ajaxSuccess: function(data){
		// 设置底部合计行数据(设置合计行)
		$('#customerSettlementPaymentDataGrid').dataGrid("footerData", "set", {
			'expenseItem' : '合计：',
			'actualAmount': "${actualAmountTotalPayment}"
		}, false);
	}
});
//初始化客户结算单预收款DataGrid对象
$("#customerSettlementPrepaymentDataGrid").dataGrid({

	data: ${toJson(customerSettlement.customerSettlementPrepaymentList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度

	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'customerSettlementPrepaymentId', editable:true, hidden:true},
		{header:'${text("预收款单号")}', name:'prepaymentCode', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'100', 'class':'form-control'}},
		{header:'${text("预收款类型")}', name:'prepaymentType', width:100,
			editable:false, edittype:'text'
		},
		{header:'${text("金额")}', name:'amount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number'}},
		{header:'${text("已核销金额")}', name:'verificationAmount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number'}},
		{header:'${text("剩余金额")}', name:'remainingAmount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number'}},
		{header:'${text("客户结算单ID")}', name:'customerSettlementId.customerSettlementId', editable:true, hidden:true},
		{header:'${text("操作")}', name:'actions', hidden: true}
	],

	// 编辑表格参数
	editGrid: false,				// 是否是编辑表格
	editGridInitRowNum: 1,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#customerSettlementPrepaymentDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {customerSettlementPrepaymentId: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据

	// 编辑表格的提交数据参数
	editGridInputFormListName: 'customerSettlementPrepaymentList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,customerSettlementPrepaymentId,prepaymentCode,prepaymentType,amount,verificationAmount,remainingAmount,customerSettlementId.customerSettlementId,', // 提交数据列表的属性字段

	// 加载成功后执行事件
	ajaxSuccess: function(data){

	}
});
//初始化客户结算单明细DataGrid对象
$("#customerSettlementDetailDataGrid").dataGrid({

	data: ${toJson(customerSettlement.customerSettlementDetailList)},
	datatype: "local", // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度

	// 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'customerSettlementDetailId', editable:true, hidden:true},
		{header:'${text("车辆")}', name:'carId', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'64', 'class':'form-control'}},
		{header:'${text("车架号")}', name:'frameNumber', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'100', 'class':'form-control'}},
		{header:'${text("车价")}', name:'price', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number'}},
		{header:'${text("币种")}', name:'currency', width:100,
			editable:true, edittype:'select', editoptions:{'class':'form-control',
				items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], ${@DictUtils.getDictListJson('sys_bizhong')}),
				itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
					$(element).select2().on("change",function(){$(this).resetValid()});
				}
			}
		},
		{header:'${text("预计汇率")}', name:'estimateExchangeRate', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'10', 'class':'form-control'}},
		{header:'${text("实际汇率")}', name:'actualExchangeRate', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'10', 'class':'form-control'}},
		{header:'${text("应收总金额")}', name:'payableAmount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number'}},
		{header:'${text("实收总金额")}', name:'actualAmount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number'}},
		{header:'${text("印花税")}', name:'stampDuty', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number'}},
		{header:'${text("总金额")}', name:'totalAmount', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number'}},
		{header:'${text("客户结算单ID")}', name:'customerSettlementId.customerSettlementId', editable:true, hidden:true},
		{header:'${text("操作")}', name:'actions', hidden: true}
	],

	showFooter:true,

	// 编辑表格参数
	editGrid: false,				// 是否是编辑表格
	editGridInitRowNum: 1,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#customerSettlementDetailDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowInitData: {customerSettlementDetailId: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据

	// 编辑表格的提交数据参数
	editGridInputFormListName: 'customerSettlementDetailList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,customerSettlementDetailId,carId,frameNumber,price,currency,estimateExchangeRate,actualExchangeRate,payableAmount,actualAmount,stampDuty,totalAmount,customerSettlementId.customerSettlementId,', // 提交数据列表的属性字段

	// 加载成功后执行事件
	ajaxSuccess: function(data){
		// 设置底部合计行数据(设置合计行)
		$('#customerSettlementDetailDataGrid').dataGrid("footerData", "set", {
			'actualExchangeRate' : '合计：',
			'payableAmount': "${payableAmountTotal}",
			'actualAmount': "${actualAmountTotal}",
			'stampDuty': "${stampDutyTotal}",
			'totalAmount': "${totalAmountTotal}"
		}, false);
	}
});
</script>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			js.showMessage(data.message);
			if(data.result == Global.TRUE){
				js.closeCurrentTabPage(function(contentWindow){
					contentWindow.page();
				});
			}
		}, "json");
    }
});
$(function(){
	let num= $("#customerPrepaymentBalance").val();
	$("#customerPrepaymentBalance").val(parseFloat(num));
	let num1= $("#orderPrepaymentBalance").val();
	$("#orderPrepaymentBalance").val(parseFloat(num1));
});
function supContract(){
	$("#btnS").css("display","none");
	$("#btnSh").click();
}
function calFinish(){
	let customerSettlementId = "${customerSettlement.customerSettlementId}";
	let date = $("#date").val();
	if(date == null || date == ""){
		js.showErrorMessage("结算日期未选择");
		return false;
	}
	let invoiceCustomerId = $("#invoiceCustomerId").val();
	if(invoiceCustomerId ==null || invoiceCustomerId==""){
		js.showErrorMessage("结算客户未选择");
		return false;
	}
	let remarks = $("#remarks").val();
	$.ajax({
		type: "post",
		url: "${ctx}/settlement/customersettlement/customerSettlement/finishSave",
		data: {customerSettlementId: customerSettlementId,date:date,invoiceCustomerId:invoiceCustomerId,remarks:remarks},
		dataType: "json",
		success: function (data) {
			if(data == ""){
				js.showErrorMessage("财务复核失败");
				return false;
			}
			$("#finishFlag").val("1");
			$("#invoiceCustomer").prop("disabled",true);
			$("#date").prop("disabled",true);
			$("#remarks").val("readonly",true);
			$("#btnF").css("display","none");
		}
	});
}
</script>
